package com.cqrt.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.dto.TransJobDTO;
import com.cqrt.entity.RTPreConstraintDO;
import com.cqrt.entity.RTPreConstraintVO;
import com.cqrt.rt.basic.request.BatchOperationRequest;
import com.cqrt.service.RTProjectService;
import com.cqrt.vo.RTExecutorJobVO;
import com.cqrt.vo.RTProjectRelationVO;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.core.dto.RequestDTO;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.model.dto.admin.TyppmProjectDTO;
import com.hustcad.plm.rpm.model.dto.job.BatchJobDTO;
import com.hustcad.plm.rpm.model.dto.job.TyppmJobDTO;
import com.hustcad.plm.rpm.model.vo.change.CreateChangeInfoRequestVO;
import com.hustcad.plm.rpm.model.vo.change.PlanChangeInfoVO;
import com.hustcad.plm.rpm.service.job.TyppmJobService;
import com.ty.basic.response.ResponseResult;
import com.ty.ppm.model.constant.ApiPathConstant;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@Api(tags = "项目")
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH + "project")
public class RTProjectController {

    private static final Logger log = LoggerFactory.getLogger(RTProjectController.class);
    @Resource
    private RTProjectService rtProjectService;

    @Resource
    private TyppmJobService typpmJobService;

    @GetMapping("/queryJobByExecutor")
    @ApiOperation(value = "查询执行人任务数量", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo<RTExecutorJobVO>> queryJobByExecutor(
            String executorOID,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        PageInfo<RTExecutorJobVO> pageInfo = rtProjectService.queryJobByExecutor(executorOID, page, pageSize);
        return ResponseResult.success(pageInfo);
    }

    @GetMapping("/updatePreconstraint")
    @ApiOperation(value = "更新前置任务交付物", produces = "application/json; charset=utf-8")
    public ResponseResult<T> updatePreconstraint(
            String activeID,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        rtProjectService.updatePreConstraint(activeID);
        return ResponseResult.success();
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入年度项目", produces = "application/json; charset=utf-8")
    public ResponseResult<T> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        String msg = "";
        try {
             msg = rtProjectService.importAnnualProject(file);
        }catch (Exception e){
           // e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }
        return ResponseResult.success(msg);
    }

    @GetMapping("/exportprojectplanexcel")
    @ApiOperation(value = "项目周期比对导出", produces = "application/json; charset=utf-8")
    public ResponseResult<?> exportProjectPlanExcel( HttpServletResponse response, String projectId) throws IOException {

        rtProjectService.exportProjectPlanExcel(response,projectId);

        return ResponseResult.success();

    }

    @GetMapping("/queryProjectRelation")
    @ApiOperation(value = "获取项目关系", produces = "application/json; charset=utf-8")
    public ResponseResult<RTProjectRelationVO> queryProjectRelation(String projectOid,
                                                                          @RequestParam(defaultValue = "1") int page,
                                                                          @RequestParam(defaultValue = "20") int pageSize) {
        RTProjectRelationVO pageInfo = rtProjectService.queryProjectRelation(projectOid, page, pageSize);
        return ResponseResult.success(pageInfo);
    }

    @PostMapping("/batchOperationProjectPreconstraint")
    @ApiOperation(value = "更新项目关联关系", produces = "application/json; charset=utf-8")
    public ResponseResult<T> batchOperationProjectPreconstraint(@RequestBody BatchOperationRequest<RTPreConstraintDO> request) {
        rtProjectService.batchInsertOrUpdateProject(request);
        return ResponseResult.success();
    }

    @GetMapping("/queryProjectPreconstraint")
    @ApiOperation(value = "查询项目关系", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo<RTPreConstraintVO>>
    queryProjectPreconstraint(
            String projectOid,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize) {
        PageInfo<RTPreConstraintDO> pageInfo = rtProjectService.queryProjectPreConstraint(projectOid, page, pageSize);
        List<RTPreConstraintVO> rtPreConstraintVOList = new ArrayList<>();
        pageInfo.getList().forEach(rtPreConstraintDO -> {
                RTPreConstraintVO rtPreConstraintVO = new RTPreConstraintVO();
                rtPreConstraintVO.setOtype(rtPreConstraintDO.getOtype());
                rtPreConstraintVO.setApname(rtPreConstraintDO.getApname());
                rtPreConstraintVO.setBpname(rtPreConstraintDO.getBpname());
                rtPreConstraintVO.setAjname(rtPreConstraintDO.getAjname());
                rtPreConstraintVO.setBjname(rtPreConstraintDO.getBjname());
                rtPreConstraintVO.setAjoid(rtPreConstraintDO.getAjoid().toString());
                rtPreConstraintVO.setBjoid(rtPreConstraintDO.getBjoid().toString());
                rtPreConstraintVO.setApoid(rtPreConstraintDO.getApoid().toString());
                rtPreConstraintVO.setBpoid(rtPreConstraintDO.getBpoid().toString());
                rtPreConstraintVO.setOid(rtPreConstraintDO.getOid().toString());
                rtPreConstraintVOList.add(rtPreConstraintVO);
        });
        PageInfo<RTPreConstraintVO> pageInfoVO = new PageInfo<>(rtPreConstraintVOList);
        return ResponseResult.success(pageInfoVO);
    }

    @PostMapping({"/saveProject"})
    @ApiOperation(
            value = "参考创建项目",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<Boolean> saveAsProject(@RequestBody TyppmProjectDTO project) {
        rtProjectService.saveAsProject(project);
        return TyppmResponseResult.success();
    }

    @PostMapping({"/transJob"})
    @ApiOperation(
            value = "执行人转交任务",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<T> transJob(@RequestBody TransJobDTO transJobDTO) {
        rtProjectService.transJob(transJobDTO);
        return ResponseResult.success();
    }

    @GetMapping({"/checkAnnualProjectAdmin"})
    @ApiOperation(
            value = "校验年度计划项目",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<T> checkAnnualProjectAdmin() {
        boolean tag=rtProjectService.checkAnnualProjectAdmin();
        return ResponseResult.success(tag);
    }


    @GetMapping({"/getPrjDescription"})
    @ApiOperation(
            value = "获取项目描述信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<String> getPrjDescription(String oid) {
        String description = rtProjectService.getPrjDescription(oid);
        return ResponseResult.success(description);
    }

    @GetMapping({"/collectCommonProjectName"})
    @ApiOperation(
            value = "标准化模板库",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<String>> collectCommonProjectName() {
        List<String> projectJobNames = rtProjectService.collectCommonProjectName();
        return ResponseResult.success(projectJobNames);
    }

    @PostMapping({"/getChangeInfo"})
    @ApiOperation(
            value = "获取创建项目流程信息",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<CreateChangeInfoRequestVO> getChangeInfo(@RequestBody RequestDTO<PlanChangeInfoVO> requestVO) {
        CreateChangeInfoRequestVO changeInfo = rtProjectService.getChangeInfo(requestVO.getParams());

        return ResponseResult.success(changeInfo);
    }

    @PostMapping({"/synchronizationParentJobOutRules"})
    @ApiOperation(
            value = "同步父任务交付物规则",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Void> synchronizationParentJobOutRules(@RequestBody JSONObject params) {
        rtProjectService.synchronizationParentJobOutRules(params);
        return ResponseResult.success();
    }
    @PostMapping({"/synchronizationJobOutRules"})
    @ApiOperation(
            value = "同步任务交付物规则",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<Void> synchronizationJobOutRules(@RequestBody @Valid JSONObject params) {
        JSONArray jobList = params.getJSONArray("jobList");
        for (int i=0;i<jobList.size();i++) {
            params.put("currentJobId",jobList.get(i));
            rtProjectService.synchronizationParentJobOutRules(params);
        }
        return ResponseResult.success();
    }
    @PostMapping({"/directlyPublishPlan"})
    @ApiOperation(
            value = "计划发布",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<CreateChangeInfoRequestVO> directlyPublishPlan(@RequestBody @Valid RequestDTO<PlanChangeInfoVO> requestVO) {
        CreateChangeInfoRequestVO changeInfo = rtProjectService.directlyPublishPlan(requestVO.getParams());
        return TyppmResponseResult.success(changeInfo);
    }

    @PostMapping({"/selectSecurity"})
    @ApiOperation(
            value = "探索秘密",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<String> directlyPublishPlan(@RequestBody @Valid String oid) {
        String selectSecurity = rtProjectService.selectSecurity(oid);
        return TyppmResponseResult.success(selectSecurity);
    }

    @GetMapping("/selectAllProjectIdentifier")
    @ApiOperation(value = "查询所有项目标识", produces = "application/json; charset=utf-8")
    public ResponseResult<List<String>> selectAllProjectIdentifier(@Param("key") String key) {
        return ResponseResult.success(rtProjectService.selectAllProjectIdentifier(key));
    }
}
